package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.mapper.CommunityMapper;
import com.bayihouse.model.entity.Community;
import com.bayihouse.model.request.community.CommunityQueryRequest;
import com.bayihouse.model.vo.CommunityVO;
import com.bayihouse.service.CommunityService;
import com.bayihouse.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【community】的数据库操作Service实现
 * @createDate 2024-09-17 10:28:03
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community>
        implements CommunityService {

    @Override
    public Wrapper<Community> getQueryWrapper(CommunityQueryRequest communityQueryRequest) {
        if (communityQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = communityQueryRequest.getId();
        String name = communityQueryRequest.getName();
        String address = communityQueryRequest.getAddress();
        String detail = communityQueryRequest.getDetail();
        String sortField = communityQueryRequest.getSortField();
        String sortOrder = communityQueryRequest.getSortOrder();
        Long netId = communityQueryRequest.getNetId();
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(StringUtils.isNotBlank(address), "address", address);
        queryWrapper.like(StringUtils.isNotBlank(detail), "detail", detail);
        queryWrapper.eq(netId != null, "father_id", netId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_DESC),
                sortField);
        return queryWrapper;
    }

    @Override
    public CommunityVO getCommunityVO(Community community) {
        if (community == null) {
            return null;
        }
        return CommunityVO.objToVo(community);
    }

    @Override
    public List<CommunityVO> getCommunityVOList(List<Community> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getCommunityVO).collect(Collectors.toList());
    }


    @Override
    public Long getByAddress(String address) {
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address", address);
        return this.getOne(queryWrapper).getId();
    }

    @Override
    public Long getByAddressAndName(String name, String address) {
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address", address);
        queryWrapper.like("name", name);
        return this.getOne(queryWrapper).getId();
    }
}




